package io.jee.medusa.invoke.sms;

import io.jee.medusa.httpclient.HttpClientHelper;
import io.jee.medusa.httpclient.HttpResult;
import io.jee.medusa.utils.ResultModel;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.util.StringUtils;

@Deprecated
public class SMSChineseService implements ShortMessageService {
	
	private HttpClientHelper httpClientHelper;
	private String uid;
	private String key;
	private String signature;
	
	public SMSChineseService(String uid, String key, String signature) {
		this.uid = uid;
		this.key = key;
		this.signature = signature;
	}

	@Override
	public ResultModel<Void> send(String signature, String text, String... mobiles) {
		List<NameValuePair> formParams = new ArrayList<NameValuePair>();
		formParams.add(new BasicNameValuePair("Uid", uid));
		formParams.add(new BasicNameValuePair("Key", key));
		String mobile = mobiles[0];
		if (mobiles.length > 1) {
			for (int i = 1; i < mobiles.length; i++) {
				mobile += "," + mobiles[i];
			}
		}
		formParams.add(new BasicNameValuePair("smsMob", mobile));
		if(StringUtils.hasText(signature)){
			text += "【"+signature+"】";
		}else{
			text += "【"+this.signature+"】";
		}
		formParams.add(new BasicNameValuePair("smsText", text));
		HttpResult result = httpClientHelper.post(
				"http://utf8.sms.webchinese.cn/", formParams);
		if (result.isSuccess()) {
			int code = Integer.parseInt(result.getContent());
			if (code > 0) {
				return new ResultModel<>(true, "发送短信成功");
			} else {
				return new ResultModel<>(false, "短信发送异常");
			}
		} else {
			return new ResultModel<>(false, "接口服务异常");
		}
	}
	
	@Override
	public ResultModel<Void> send(String text, String... mobiles) {
		return send(null, text, mobiles);
	}
	
	@Override
	public ResultModel<Void> send(String text, String mobile) {
		return send(text, new String[]{mobile});
	}
	
	@Override
	public ResultModel<Void> send(String signature, String text, List<String> mobiles) {
		return send(signature, text, (String[]) mobiles.toArray());
	}

	@Override
	public ResultModel<Void> send(String text, List<String> mobiles) {
		return send(text, (String[]) mobiles.toArray());
	}

	@Resource
	public void setHttpClientHelper(HttpClientHelper httpClientHelper) {
		this.httpClientHelper = httpClientHelper;
	}
	
}
